package com.livepan.generator.server;

import com.livepan.generator.util.DbUtil;
import com.livepan.generator.util.Field;
import com.livepan.generator.util.FreemarkerUtil;
import freemarker.template.TemplateException;
import org.bouncycastle.math.ec.custom.sec.SecT113Field;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class ServerGenerator {
    static String MODULE = "system";
    static String toServicePath = "server\\src\\main\\java\\com\\livepan\\server\\service\\";
    static String toModelPath = "server\\src\\main\\java\\com\\livepan\\server\\service\\model\\";
    static String toControllerPath = "\\src\\main\\java\\top\\livepan\\" + MODULE + "\\controller\\";
    static String generatorConfigPath = "server\\src\\main\\resources\\mybatis-generator.xml";

    public static void main(String[] args) throws Exception {

        String module = MODULE;
        // 只生成配置文件中的第一个table节点
        File file = new File(generatorConfigPath);
        SAXReader reader = new SAXReader();
        //读取xml文件到Document中
        Document doc = reader.read(file);
        //获取xml文件的根节点
        Element rootElement = doc.getRootElement();
        //读取context节点
        Element contextElement = rootElement.element("context");
        //定义一个Element用于遍历
        Element tableElement;
        //取第一个“table”的节点
        tableElement = contextElement.elementIterator("table").next();
        String Domain = tableElement.attributeValue("domainObjectName").replace("DO", "");
        String tableName = tableElement.attributeValue("tableName");
        String tableNameCn = DbUtil.getTableComment(tableName);
        String domain = Domain.substring(0, 1).toLowerCase() + Domain.substring(1);
        System.out.println("表：" + tableElement.attributeValue("tableName"));
        System.out.println("Domain：" + tableElement.attributeValue("domainObjectName"));


        List<Field> fieldList = DbUtil.getColumnByTableName(tableName);
        Set<String> typeSet = getJavaTypes(fieldList);
        Map<String, Object> map = new HashMap<>();
        map.put("Domain", Domain);
        map.put("domain", domain);
        map.put("tableNameCn", tableNameCn);
        map.put("MODULE", MODULE);
        map.put("fieldList", fieldList);
        map.put("typeSet", typeSet);
        FreemarkerUtil.initConfig("model.ftl");
        FreemarkerUtil.generator(toModelPath + Domain + "Model.java", map);

        FreemarkerUtil.initConfig("service.ftl");
        FreemarkerUtil.generator(toServicePath + Domain + "Service.java", map);

        FreemarkerUtil.initConfig("controller.ftl");
        FreemarkerUtil.generator(MODULE + toControllerPath + Domain + "Controller.java", map);


    }

    /**
     * 获取所有的Java类型，使用Set去重
     */
    private static Set<String> getJavaTypes(List<Field> fieldList) {
        Set<String> set = new HashSet<>();
        for (Field field : fieldList) {
            set.add(field.getJavaType());
        }
        return set;
    }
}
